package com.xy;

import com.xy.command.Command;
import com.xy.command.NoCommand;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/16 20:30
 * @description
 */
public class RemoteController {
    // 开命令数组
    private Command[] onCommands;
    // 关命令数组
    private Command[] offCommands;

    // 撤销命令位置
    private int no;
    // 撤销命令是否为按下命令
    private Boolean isOn;

    /**
     * 初始化
     */
    public RemoteController() {
        this.onCommands = new Command[5];
        this.offCommands = new Command[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }


    /**
     * 设置一行中的按钮
     * @param no
     * @param onCommand
     * @param offCommand
     */
    public void setCommand(int no,Command onCommand,Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 开按钮按下
     * @param no
     */
    public void onButtonPushed(int no){
        // 调用按钮方法
        onCommands[no].execute();
        // 记录撤销按钮
        this.no = no;
        isOn = true;
    }

    /**
     * 关按钮按下
     * @param no
     */
    public void offButtonPushed(int no) {
        // 调用按钮方法
        offCommands[no].execute();
        // 记录撤销按钮
        this.no = no;
        isOn = false;
    }

    public void undo() {
        if (isOn) {
            onCommands[no].undo();
        } else {
            offCommands[no].undo();
        }
        isOn = !isOn;
    }
}
